package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.*;
import com.ketangpai.admin.entity.*;
import com.ketangpai.admin.entity.dto.CourseDTO;
import com.ketangpai.admin.entity.dto.TopCourseDTO;
import com.ketangpai.admin.entity.param.*;
import com.ketangpai.admin.service.UserCourseService;
import com.ketangpai.admin.utils.Encrypt;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserCourseImpl extends ServiceImpl<UserCourseDao, UserCourse> implements UserCourseService {
    private UserCourseDao userCourseDao;
    private CourseDao courseDao;
    private UserDao userDao;
    private WorkDao workDao;
    private StuWorkDao stuWorkDao;
    public UserCourseImpl(UserCourseDao userCourseDao, CourseDao courseDao, UserDao userDao, WorkDao workDao) {
        this.userCourseDao = userCourseDao;
        this.courseDao = courseDao;
        this.userDao = userDao;
        this.workDao = workDao;
    }

    //获取该用户的置顶课程
    @Override
    public List<TopCourseDTO> getTopCourse(User user) {
        List<UserCourse> userCourses=getAll(user);
        List topCourses=new ArrayList<>();
        for(int i=0;i<userCourses.size();i++){
            if(userCourses.get(i).getIsTop()==0&&userCourses.get(i).getIsPigeonhole()==1){
                topCourses.add(userCourses.get(i));
            }
        }
        List<TopCourseDTO> courses=new ArrayList<>();
        for (int i=0;i<topCourses.size();i++){
            Course course=courseDao.getTopCourseById((UserCourse) topCourses.get(i));
            User actor=userDao.getById(course.getUserId());
            List students=getStudents(course.getId());
            Integer number=students.size();
            TopCourseDTO topCourseDTO=new TopCourseDTO(course,((UserCourse) topCourses.get(i)).getType(),actor.getName(),number);
            courses.add(topCourseDTO);
        }
        return courses;
    }

    @Override
    public List<UserCourse> getAll(User user) {
        System.out.println("getAll:"+ user.getId());
        List<UserCourse> courses=userCourseDao.getById(user.getId());
        return courses;
    }

    @Override
    //更改其置顶属性以及归档
    public boolean changeIsTop(ChangeParam changeParam) {
//        userCourseDao.update()
        //先根据useId和courseId找到user_course表中对应数据
        try {
            UserCourse userCourse=userCourseDao.getByUserAndCourse(changeParam.getUserId(),changeParam.getCourseId());
            userCourse.setIsTop(changeParam.getIsTop());
            userCourse.setIsPigeonhole(changeParam.getIsPigeonhole());
            userCourseDao.updateById(userCourse);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
//        userCourseDao.update()
    }

    @Override
    public String dropCourse(DropCourseParam dropCourseParam) {
        try {
            User user=userDao.getById(dropCourseParam.getUserId());
            String password= Encrypt.AESEncrypt(dropCourseParam.getPassword());
            if(password.equals(user.getPassword())){
                UserCourse userCourse=userCourseDao.getByUserAndCourse(dropCourseParam.getUserId(),dropCourseParam.getCourseId());
                List<Work> works=workDao.selectByCourseId(userCourse.getCourseId());
                for (int i=0;i<works.size();i++){
                    stuWorkDao.deleteByWidUid(works.get(i).getId(),user.getId());
                }
                userCourseDao.deleteById(userCourse.getId());

                return "退课成功";
            } else return "密码错误";
        }catch (Exception e){
            e.printStackTrace();
            return "退课失败";
        }
    }

    @Override
    public String joinCourse(JoinCourseParam joinCourseParam) {
        Course course=courseDao.getByCourseCode(joinCourseParam);
        if(course==null){
            return "找不到课程";
        }else {
            UserCourse userCourse=userCourseDao.getByUserAndCourse(joinCourseParam.getUserId(),course.getId());
            if(userCourse!=null){
                return "已经加入该课程了";
            }else {
                UserCourse newUserCourse=new UserCourse();
                newUserCourse.setCourseId(course.getId());
                newUserCourse.setUserId(joinCourseParam.getUserId());
                newUserCourse.setIsTop(0);
                newUserCourse.setIsPigeonhole(1);
                newUserCourse.setGroupId(0);
                newUserCourse.setEnterTime(new Date());
                newUserCourse.setType(0);
                userCourseDao.insert(newUserCourse);
                return "加入课程成功";
            }
        }
    }

    @Override
    public Map<String,List<CourseDTO>> getCourseByType(CourseByTypeParam courseByTypeParam) {
        User user=userDao.getById(courseByTypeParam.getUserId());
        Map<String,List<CourseDTO>> courseListByTime=new HashMap<>();

        List<UserCourse> userCourses=getAll(user);
        for (int i=0;i<userCourses.size();i++){
            if(userCourses.get(i).getType()==courseByTypeParam.getType()&&userCourses.get(i).getIsPigeonhole()==1){
                    Course course=courseDao.selectById(userCourses.get(i).getCourseId());
                    String key=course.getAcademicYear()+" "+course.getSemester();
                    UserCourse userCourse=userCourses.get(i);
                    List students=getStudents(course.getId());
                    Integer number=students.size();
                    //如何存在相同的学年和学期，就存储到对应的数组里面
                    if(courseListByTime.containsKey(key)){
                        CourseDTO courseDTO=new CourseDTO(courseDao.selectById(userCourse.getCourseId()),courseByTypeParam.getType(),user.getName(),userCourse.getIsTop(),userCourse.getIsPigeonhole(),number);
                        courseListByTime.get(key).add(courseDTO);
                    }else {
                        List<CourseDTO> list=new ArrayList<>();
                        CourseDTO courseDTO=new CourseDTO(courseDao.selectById(userCourse.getCourseId()),courseByTypeParam.getType(),user.getName(),userCourse.getIsTop(),userCourse.getIsPigeonhole(),number);
                        list.add(courseDTO);
                        courseListByTime.put(key,list);

                    }
            }
        }
        return courseListByTime;
    }
    //获取归档课程
    public Map<String,List<CourseDTO>> getPigeonholeCourse(CourseByTypeParam courseByTypeParam){
        User user=userDao.getById(courseByTypeParam.getUserId());
        Map<String,List<CourseDTO>> courseListByTime=new HashMap<>();
        List<UserCourse> userCourseList=getAll(user);
        for (int i=0;i<userCourseList.size();i++){
            if(userCourseList.get(i).getType()==courseByTypeParam.getType()&&userCourseList.get(i).getIsPigeonhole()==0){
                Course course=courseDao.selectById(userCourseList.get(i).getCourseId());
                //获取该课程的学生个数
                List students=getStudents(course.getId());
                Integer number=students.size();
                String key=course.getAcademicYear()+" "+course.getSemester();
                UserCourse userCourse=userCourseList.get(i);
                //如何存在相同的学年和学期，就存储到对应的数组里面
                if(courseListByTime.containsKey(key)){
                    CourseDTO courseDTO=new CourseDTO(courseDao.selectById(userCourse.getCourseId()),courseByTypeParam.getType(),user.getName(),userCourse.getIsTop(),userCourse.getIsPigeonhole(),number);
                    courseListByTime.get(key).add(courseDTO);
                }else {
                    List<CourseDTO> list=new ArrayList<>();
                    CourseDTO courseDTO=new CourseDTO(courseDao.selectById(userCourse.getCourseId()),courseByTypeParam.getType(),user.getName(),userCourse.getIsTop(),userCourse.getIsPigeonhole(),number);
                    list.add(courseDTO);
                    courseListByTime.put(key,list);

                }
            }

        }
        return courseListByTime;
    }

    @Override
    public List<User> getTeachers(Integer courseId) {
        List teachersIds=userCourseDao.getTeachersId(courseId);

        List<User> users=new ArrayList<>();
        for (int i=0;i<teachersIds.size();i++){
            User user=userDao.getById((Integer) teachersIds.get(i));
            System.out.println(teachersIds.get(i));
            users.add(user);
        }
        return users;
    }

    @Override
    public List<User> getStudents(Integer courseId) {
        List studentsIds=userCourseDao.getStudentsId(courseId);
        List<User> users=new ArrayList<>();
        for (int i=0;i<studentsIds.size();i++){
            User user=userDao.getById((Integer) studentsIds.get(i));
            users.add(user);
        }
        return users;
    }

    public boolean addTeacher(AddMemberParam addMemberParam){
        Course course=courseDao.selectById(addMemberParam.getCourseId());
        UserCourse userCourse=new UserCourse(course);
        User user=userDao.getByAccount(addMemberParam.getAccount());
        if(user==null){
            return false;
        }else {
            userCourse.setUserId(user.getId());
            userCourse.setType(2);
            userCourse.setEnterTime(new Date());
            userCourseDao.insert(userCourse);
            return true;
        }

    }

    @Override
    public boolean changeIsPigeonholeAll(Integer courseId) {
        System.out.println("courseId:"+courseId);
        List<UserCourse> userCourses=userCourseDao.getByCid(courseId);

        for (int i=0;i<userCourses.size();i++){
            System.out.println(userCourses.get(i));
            userCourses.get(i).setIsPigeonhole(0);
            userCourseDao.updateById(userCourses.get(i));
        }
        return true;

    }

    @Override
    public UserCourse getList(GetUserCourseParam getUserCourseParam) {
        return userCourseDao.getByUserAndCourse(getUserCourseParam.getUserId(),getUserCourseParam.getCourseId());
    }

    @Override
    public boolean deleteStudent(DeleteStudentParam deleteStudentParam) {
        UserCourse userCourse=userCourseDao.getByUserAndCourse(deleteStudentParam.getUserId(),deleteStudentParam.getCourseId());
        List<Work> works=workDao.selectByCourseId(userCourse.getCourseId());
        for (int i=0;i<works.size();i++){
            stuWorkDao.deleteByWidUid(works.get(i).getId(),userCourse.getUserId());
        }
        return userCourseDao.deleteById(userCourse.getId());
    }
}
